
import React from 'react';

import styles from './login.css';
import {Button, Icon, Input, message} from "antd";
import LoginService from "../../../business/login/LoginService";
import {ResponseBody} from "../../../components/http/ResponseBody";


class Login extends React.Component{

  constructor(props) {
    super(props);
    this.state ={
      username:'',
      password:'',
    }
    this.loginService = new LoginService();
    this.handleChange = this.handleChange.bind(this);
    this.hasErrors =this.hasErrors.bind(this);
    this.handleSubmit = this.handleSubmit.bind(this);
    this.adminLoginCallBack = this.adminLoginCallBack.bind(this);

  }

  loginService :LoginService;

  handleSubmit = (e) => {
    e.preventDefault();
    let username = this.state["username"];
    let password = this.state["password"];
    if(username == undefined ||username == ''){
      message.info("用户名不能为空");
      return;
    }
    if(password == undefined ||password == ''){
      message.info("密码不能为空");
      return;
    }
    const data ={
      "username":username,
      "password":password,
    };

    this.loginService.adminLogin(data,this.adminLoginCallBack);
  }

  adminLoginCallBack(data:ResponseBody<any>,isSuccess,req){
    if(!isSuccess){
      message.info(data.message);
    }else{
      if(data.code == "200"){
        sessionStorage.setItem("adminLoginToken",data.data.toString());
        sessionStorage.setItem("adminLsername",req.username);
        window.document.location.href='/point-shopping/admin';
      }else {
        message.info(data.message);
      }
    }
    this.setState({ loading: false });
  }
  handleChange(event) {
    this.setState({ [event.target.name] : event.target.value});
  }

  hasErrors(){
    let username = this.state["username"];
    let password = this.state["password"];
    if(username == undefined ||username == ''){
      return true;
    }
    if(password == undefined ||password == ''){
      return true;
    }
    return false;

  }

  render() {
    return (
      <div className={styles.normal}>
        {/*<div className={styles.login_page}>
          <div className={styles.login_boforeBox}>
            <div className={styles.login_box}>
              <div className={styles.login_input}>
                <Form onSubmit={this.handleSubmit} className={styles["login-form"]}>
                  <div className={styles.login_div}>
                    <Form.Item>
                      <Input prefix={<Icon type="user" style={{color: 'rgba(0,0,0,.25)'}}/>}
                             placeholder="Username" value={this.state["username"]}
                             name='username' onChange={this.handleChange}/>

                    </Form.Item>
                  </div>
                  <div className={styles.login_div}>
                    <Form.Item>
                      <Input prefix={<Icon type="lock" style={{color: 'rgba(0,0,0,.25)'}}/>}
                             type="password" placeholder="Password" value={this.state["password"]}
                             name='password' onChange={this.handleChange}/>

                    </Form.Item>
                  </div>
                  <div>
                    <Form.Item>
                      <Button
                        type="primary"
                        htmlType="submit"
                        className={styles["login-form-button"]}
                      >
                        登 陆
                      </Button>
                    </Form.Item>
                  </div>
                </Form>
              </div>
            </div>
          </div>
        </div>
        */}
      </div>
    );
  }
}

export default Login;
